package com.chenshang.controller;

import java.io.File;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.runner.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.chenshang.util.FileReaderUtil;
import com.chenshang.util.FileWriterUtil;
import com.chenshang.util.JaxbUtil;
import com.google.gson.Gson;
import com.sun.org.apache.xalan.internal.xsltc.compiler.sym;
import com.travelzen.flight.base.TzRequest;
import com.travelzen.flight.base.TzResponse;

@Controller
public class LoginCtrl {
	public static final Logger LOGGER = (Logger) LoggerFactory.getLogger(InvokCtrl.class);
	private static final Gson GSON = new Gson();

	@RequestMapping(value = { "/index", "/testlog" })
	public String login() {
		return "index";
	}

	@RequestMapping(value = { "/index3" })
	public String login3() {
		return "index3";
	}

	@RequestMapping(value = { "/xmlTojson" }, method = { RequestMethod.POST })
	public static String xmlTojson(String request, String description) {

		File filexml = new File("/home/chenshang/workspace/xml.txt");
		File filejson = new File("/home/chenshang/workspace/json.txt");

		TzRequest tzRequest = JaxbUtil.convertToTzRequestByXml(request);
		String type = "";
		try {
			Document document = DocumentHelper.parseText(request.trim());
			Element root = document.getRootElement();
			Element requestEntity = root.element("requestEntity");
			type = requestEntity.attributeValue("type");
			String[] strings = type.split(":");
			type = strings[1];
		} catch (DocumentException e) {
			e.printStackTrace();
		}

		String json = null;
		if (tzRequest != null) {
			LOGGER.info("开始转换");
			json = GSON.toJson(tzRequest);
			json = json.replace("requestEntity", type);
			LOGGER.info("转换结果" + json);
		}
		FileWriterUtil.writeToFile(filexml, description + "\n" + request + "\n");
		FileWriterUtil.writeToFile(filejson, description + "\n" + json + "\n");

		return "index3";
	}

	public static void main(String[] args) {
		File filexml = new File("/home/chenshang/workspace/xmltest.txt");

		SAXReader reader = new SAXReader();
		try {
			// Document document = reader.read(filexml);
			Document document = DocumentHelper.parseText(FileReaderUtil.readFileToString(filexml).trim());
			Element root = document.getRootElement();
			Element requestEntity = root.element("requestEntity");
			String type = requestEntity.attributeValue("type");

			String[] strings = type.split(":");
			type = strings[1];
			System.out.println(type);
		} catch (DocumentException e) {
			e.printStackTrace();
		}

	}

	@RequestMapping(value = { "/rexmlTojson" }, method = { RequestMethod.POST })
	public String reXmlTojson(String response, String description) {

		File filexml = new File("/home/chenshang/workspace/rexml.txt");
		File filejson = new File("/home/chenshang/workspace/rejson.txt");

		TzResponse tzResponse = JaxbUtil.convertToTzResponseByXml(response);
		String rejson = null;
		if (tzResponse != null) {
			LOGGER.info("开始转换");
			rejson = GSON.toJson(tzResponse);
			LOGGER.info("转换结果" + rejson);
		}
		FileWriterUtil.writeToFile(filexml, description + "\n" + response + "\n");
		FileWriterUtil.writeToFile(filejson, description + "\n" + rejson + "\n");

		return "index3";
	}

}
